package org.aaron.wbclient.oauth;

import oauth.signpost.OAuthConsumer;
import oauth.signpost.OAuthProvider;
import oauth.signpost.exception.OAuthCommunicationException;
import oauth.signpost.exception.OAuthExpectationFailedException;
import oauth.signpost.exception.OAuthMessageSignerException;
import oauth.signpost.exception.OAuthNotAuthorizedException;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.util.Log;

public class OAuthRequestTokenTask extends AsyncTask<Void, Void, Void> {
	private OAuthConsumer oAuthConsumer;
	private OAuthProvider oAuthProvider;
	private Context context;

	public OAuthRequestTokenTask(OAuthConsumer oAuthConsumer, OAuthProvider oAuthProvider, Context context) {
		super();
		this.oAuthConsumer = oAuthConsumer;
		this.oAuthProvider = oAuthProvider;
		this.context = context;
	}

	@Override
	protected Void doInBackground(Void... params) {

		Log.d("Before Request Token:", oAuthConsumer.getToken() + "");
		try {
			String url = oAuthProvider.retrieveRequestToken(oAuthConsumer, Constans.OAUTH_CALLBACK_URL);
			Log.d("After Request Token:", oAuthConsumer.getToken());
			Log.d("URL:", url);
			Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)).setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
			context.startActivity(intent);
		} catch (OAuthMessageSignerException e) {
			e.printStackTrace();
		} catch (OAuthNotAuthorizedException e) {
			e.printStackTrace();
		} catch (OAuthExpectationFailedException e) {
			e.printStackTrace();
		} catch (OAuthCommunicationException e) {
			e.printStackTrace();
		}
		return null;
	}

}
